using Configs;
using UnityEngine;

namespace Modules.Game.Skills
{
    public class SkillGuide : MonoBehaviour
    {
        public LineRenderer lineRenderer;
        public GameObject skillRect;
        public GameObject skillCircle;

        private readonly int _drawCircleSegmentNum = 180;
        private float _circleRadius;

        private SkillRange _range;

        private void Awake()
        {
            lineRenderer.positionCount = _drawCircleSegmentNum;
            lineRenderer.startColor = lineRenderer.endColor = Color.cyan;
        }

        private void Update()
        {
            UpdateSkillGuide();
        }

        /// <summary>
        ///     设置技能范围
        /// </summary>
        /// <param name="range"></param>
        public void SetSkillRange(SkillRange range)
        {
            _range = range;

            if (range.Rect != default)
            {
                skillCircle.SetActive(false);

                skillRect.SetActive(true);
                skillRect.transform.localScale = new Vector3(range.Rect.x, 0.1f, range.Rect.y);
                skillRect.transform.localPosition = new Vector3(0.0f, 0.0f, range.Rect.y / 2);

                _circleRadius = range.Rect.x / 2 > range.Rect.y ? range.Rect.x / 2 : range.Rect.y;
            }
            else
            {
                skillRect.SetActive(false);

                skillCircle.SetActive(true);
                skillCircle.transform.localScale = new Vector3(range.Radius * 2, 0.1f, range.Radius * 2);

                _circleRadius = range.Radius;
            }
        }

        /// <summary>
        ///     获取技能范围
        /// </summary>
        /// <returns></returns>
        public float GetSkillRange()
        {
            return _circleRadius;
        }

        private void UpdateSkillGuide()
        {
            // 刷新位置
            var heroPos = GameModule.Instance.GetHeroPos();
            transform.position = heroPos;

            // 刷新引导旋转
            UpdateGuideRot(heroPos);

            // 绘制引导圈
            DrawGuideCircle(heroPos);
        }

        private void UpdateGuideRot(Vector3 heroPos)
        {
            var cameraToHero = heroPos - UnityEngine.Camera.main!.transform.position;
            var mousePos = Input.mousePosition;
            mousePos.z = cameraToHero.magnitude;
            var screenMousePos = UnityEngine.Camera.main!.ScreenToWorldPoint(mousePos);

            var toMouseVec = screenMousePos - heroPos;
            toMouseVec.y = 0;
            transform.rotation = Quaternion.FromToRotation(Vector3.forward, toMouseVec);
        }

        private void DrawGuideCircle(Vector3 centerPos)
        {
            var toCenter = new Vector3(0.0f, 0.0f, _circleRadius);

            var rotation = Quaternion.AngleAxis(360f / (_drawCircleSegmentNum - 1), Vector3.up);
            for (var i = 0; i < _drawCircleSegmentNum; i++)
            {
                toCenter = rotation * toCenter;
                lineRenderer.SetPosition(i, centerPos + toCenter);
            }
        }
    }
}